#include "common.h"
#include "systick.h"

extern int core_clk_khz;

void systick_init(void){
    SYST_CSR=(!SysTick_CSR_ENABLE_MASK    // Disable SysTick
             | SysTick_CSR_CLKSOURCE_MASK // SysTick uses the processor clock
            );
    SYST_RVR=core_clk_khz;
}

void systick_delay_ms(int count_val){
    /* check if delay ms is reachable */
    if(count_val<0){
        printf("invalid argument for systick_delay_ms\n");
        while(1);
    }
    while(count_val--){
        SYST_CVR=0x00;
        SYST_CSR|=SysTick_CSR_ENABLE_MASK;
        while(!(SYST_CSR&SysTick_CSR_COUNTFLAG_MASK));
    }
}
